#include "clearview.h"
#include "toolkit.h"
#include "item/imageitem.h"
#include <QMouseEvent>
#include <QLocale>
#include "firework/firework.h"

FireWork firework[3];

ClearView::ClearView(QWidget *parent) :
    QGraphicsView(parent),
    m_oldy(0.0)
{
    m_bgImage = new QImage(640,360,QImage::Format_RGB32);
    m_bgImage->fill(0);

    setFrameShape(QFrame::NoFrame);

    m_Scene = new QGraphicsScene(this);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setScene(m_Scene);
    m_Scene->setSceneRect(0.0, 0.0, 640.0, 360.0);
    setGeometry(0.0,0.0,640.0,360.0);

    switch(ToolKit::m_language)
    {
    case QLocale::Chinese:
    {
        m_textItem = new ImageItem(QImage(":/image/about/ch.png"),
                                 QRect(QPoint(0,0),QImage(":/image/about/ch.png").size()));
    }
        break;
    case QLocale::HongKong:
    case QLocale::Taiwan:
    {
        m_textItem = new ImageItem(QImage(":/image/about/chh.png"),
                                 QRect(QPoint(0,0),QImage(":/image/about/chh.png").size()));
    }
        break;
    default:
    {
        m_textItem = new ImageItem(QImage(":/image/about/en.png"),
                                 QRect(QPoint(0,0),QImage(":/image/about/en.png").size()));
    }
        break;
    }
    m_textItem->setPos(110.0,50.0);
    m_textItem->setZValue(2.0);
    m_Scene->addItem(m_textItem);

//    m_bgItem = new ImageItem(QImage(":/image/gamebg/gamebg3.jpg"),
//                             QRect(QPoint(0,0),QImage(":/image/gamebg/gamebg3.jpg").size()));
//    m_bgItem->setPos(0.0,0.0);
//    m_bgItem->setZValue(0.0);
//    m_Scene->addItem(m_bgItem);

    m_bgItem = new ImageItem(*m_bgImage,
                             QRect(QPoint(0,0),m_bgImage->size()));
    m_bgItem->setPos(0.0,0.0);
    m_bgItem->setZValue(0.0);
    m_Scene->addItem(m_bgItem);

//    m_bgItem1 = new ImageItem(QImage(":/image/gamebg/dimian.jpg"),
//                              QRect(QPoint(0,0),QImage(":/image/gamebg/dimian.jpg").size()));
//    m_bgItem1->setPos(0.0,319.0);
//    m_bgItem1->setZValue(2.0);
//    m_Scene->addItem(m_bgItem1);

    m_backItem = new ImageItem(QImage(":/image/about/about_back.png"),
                              QRect(QPoint(0,0),QImage(":/image/about/about_back.png").size()));
    m_backItem->setPos(610.0,10.0);
    m_backItem->setZValue(3.0);
    m_Scene->addItem(m_backItem);

    m_timer.setInterval(100);
    connect(&m_timer,SIGNAL(timeout()),this,SLOT(AutoPlay()));
    m_timer.start();

    setViewportUpdateMode(QGraphicsView::NoViewportUpdate);
}

void ClearView::AutoPlay()
{
    if(m_textItem->y() > -860)
        m_textItem->setY(m_textItem->y() - 3);

    update();
}

void ClearView::mousePressEvent(QMouseEvent *event)
{
    if(QRectF(580,0,60,60).contains(event->pos()))
    {
        emit back();
    }
    else
    {
        m_timer.stop();
        m_oldy = event->pos().y();
    }
}

void ClearView::mouseMoveEvent(QMouseEvent *event)
{
    m_textItem->setY(m_textItem->y() + event->pos().y() - m_oldy);
    qWarning("mouseMove y %f",m_textItem->y());
    if(m_textItem->y() > 50)
    {
        m_textItem->setY(50);
    }
    else if(m_textItem->y() < -860)
    {
        m_textItem->setY(-860);
    }

    m_oldy = event->pos().y();

    update();
}

void ClearView::mouseReleaseEvent(QMouseEvent *event)
{
    m_timer.start();
}
